<script setup lang="ts"> 
import { ref } from 'vue';
import { API_CONFIG } from '../../config/apiConfig';

const emit = defineEmits(['user-added', 'close']);

const formData = ref({
  username: '',
  password: '',
  confirmPassword: '',
  nickname: ''
});

const successMessage = ref('');
const errorMessage = ref('');

const resetForm = () => {
  formData.value = {
    username: '',
    password: '',
    confirmPassword: '',
    nickname: ''
  };
};

const handleSubmit = async () => {
  if (!formData.value.username || !formData.value.password || !formData.value.confirmPassword || !formData.value.nickname) {
    errorMessage.value = '账号、密码、确认密码和昵称不能为空';
    return;
  }
  
  if (formData.value.password.length < 6) {
    errorMessage.value = '密码长度不能少于6位';
    return;
  }

  if (formData.value.password !== formData.value.confirmPassword) {
    errorMessage.value = '两次输入的密码不一致';
    return;
  }
  
  try {
    const response = await fetch(`${API_CONFIG.BASE_URL}/user`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        username: formData.value.username,
        password: formData.value.password,
        nickname: formData.value.nickname
      }),
      credentials: 'include'
    });
    const result = await response.json();

    if (result.code === '0') {
      successMessage.value = '用户创建成功！';
      resetForm();
      setTimeout(() => {
        successMessage.value = '';
      }, 3000);
      emit('user-added');
    } else {
      errorMessage.value = result.msg || '创建用户失败';
    }
  } catch (error) {
    console.error('创建用户失败:', error);
    errorMessage.value = '网络请求失败，请稍后重试';
  }
};
</script>

<template>
  <div class="add-user-form">
    <form @submit.prevent="handleSubmit">
      <div class="form-group">
        <label for="username">账号</label>
        <input 
          id="username" 
          v-model="formData.username" 
          type="text" 
          required
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input 
          id="password" 
          v-model="formData.password" 
          type="password" 
          required
        />
      </div>
      <div class="form-group">
        <label for="confirmPassword">确认密码</label>
        <input 
          id="confirmPassword" 
          v-model="formData.confirmPassword" 
          type="password" 
          required
          placeholder="请再次输入密码"
        />
      </div>
      <div class="form-group">
        <label for="nickname">昵称</label>
        <input 
          id="nickname" 
          v-model="formData.nickname" 
          type="text" 
          required
        />
      </div>
      <div class="form-actions">
        <button type="button" class="cancel-btn" @click="$emit('close')">取消</button>
        <button type="submit" class="submit-btn">添加用户</button>
      </div>
      <div v-if="successMessage" class="success-message">
        {{ successMessage }}
      </div>
      <div v-if="errorMessage" class="error-message">
        {{ errorMessage }}
      </div>
    </form>
  </div>
</template>



<style scoped>
.add-user-form {
  padding: 20px;
  max-width: 500px;
  margin: 0 auto;
}

.form-group {
  margin-bottom: 15px;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: 500;
}

input {
  width: 100%;
  padding: 8px;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
}

.cancel-btn {
  background-color: #f3f4f6;
  color: #4b5563;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  padding: 10px 15px;
  cursor: pointer;
  transition: background-color 0.2s;
}

.cancel-btn:hover {
  background-color: #e5e7eb;
}

.submit-btn {
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 4px;
  padding: 10px 15px;
  cursor: pointer;
}

.success-message {
  color: #15803d;
  margin-top: 10px;
}

.error-message {
  color: #b91c1c;
  margin-top: 10px;
}
</style>